package com.thingple.tagservice

import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.app.ActivityCompat
import android.util.Log
import android.view.View
import com.thingple.tagservice.device.DeviceCategory
import com.thingple.tagservice.device.vendor.TagArea
import com.thingple.tagservice.view.ReadCardActivity
import kotlinx.android.synthetic.main.activity_main.*
import java.util.ArrayList

class MainActivity : AppCompatActivity() {

    private val permissions = ArrayList<String>()

    private fun pms() {
        permissions.add("android.permission.VIBRATE")
        permissions.add("com.thingple.tag.permission.inventory")
        permissions.add("com.thingple.tag.permission.heartbeat")
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        pms()
        handlePermissions()
    }

    fun readTid(view: View) {
        val epc = this.label_epc.text.toString()
        if (epc.trim() == "") {
            return
        }
        val intent = Intent()
        intent.setClass(this, ReadCardActivity::class.java)
        val bundle = Bundle()
        bundle.putString("category", DeviceCategory.UHF)
        bundle.putInt("power", 20)
        bundle.putString("filter", epc)
        bundle.putInt("bank", TagArea.TID.bank)
        bundle.putInt("begin", 0)
        bundle.putInt("words", 6)

        intent.putExtras(bundle)
        startActivityForResult(intent, 0)
    }

    fun readEpc(view: View) {

        val intent = Intent()
        intent.setClass(this, ReadCardActivity::class.java)
        val bundle = Bundle()
        bundle.putInt("power", 15)
        intent.putExtras(bundle)
        startActivityForResult(intent, 0)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (data != null) {
            val bundle = data.extras
            if (bundle != null) {
                val epc = bundle.getString("EPC")
                if (epc != null) {
                    this.label_epc.text = epc

                }
                val tid = bundle.getString("TID")
                if (tid != null) {
                    this.label_tid.text = tid
                }
            }
        }
    }

    private fun handlePermissions(): Boolean {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val index = 0
            var allGranted = true
            for (permission in permissions) {
                allGranted = isGranted(permission)
                if (!allGranted) {
                    break
                }
            }
            Log.d("permission", "request permissions")
            if (!allGranted) {
                val pers = arrayOfNulls<String>(permissions.size)
                permissions.toTypedArray()
                ActivityCompat.requestPermissions(this, pers, index)
            }
            return allGranted// 无奈只能下次执行
        }
        return true
    }

    private fun isGranted(permission: String): Boolean {
        val checkSelfPermission = ActivityCompat.checkSelfPermission(this, permission)
        return checkSelfPermission == PackageManager.PERMISSION_GRANTED
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // TODO
        }
    }
}
